﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using MvcEngine.Core.Configuration;
using MvcEngine.Core;

using MvcEngine.Core.Helpers;
using MvcEngine.Core.Repositories;
using MvcEngine.Core.Content;
using MvcEngine.Mvc.View;

namespace MvcEngine.Mvc.Extensions
{
    public static class UrlHelperExtensions
    {
        public static string ControlCommand(this UrlHelper urlHelper, int ctrId, string name)
        {
            return urlHelper.RouteUrl("BackOffice_ControlCommand", new { ctrId = ctrId, name = name });
        }

        public static string RestartApplication(this UrlHelper urlHelper) 
        {
            return urlHelper.RouteUrl("BackOffice_RestartApplication");
        }

        #region Paths
        public static string AbsolutePath(this UrlHelper urlHelper, string relativeUrl)
        {
            Uri url = urlHelper.RequestContext.HttpContext.Request.Url;
            UriBuilder uriBuilder = new UriBuilder(url.Scheme, url.Host, url.Port) { Path = relativeUrl };
            return uriBuilder.Uri.ToString();
        }

        public static string AppPath(this UrlHelper urlHelper, string relativePath)
        {
            if (relativePath == null) return null;

            return VirtualPathUtility.ToAbsolute(relativePath, urlHelper.RequestContext.HttpContext.Request.ApplicationPath);
        }

        #endregion

        #region Css

        public static string GetModuleCssPath(this UrlHelper urlHelper, string cssFileName)
        {
            return urlHelper.GetModuleCssPath(cssFileName, (string)urlHelper.RequestContext.HttpContext.Items["moduleName"]);
        }

        public static string GetModuleCssPath(this UrlHelper urlHelper, string cssFileName, string moduleName)
        {
            string area = (string)urlHelper.RequestContext.RouteData.DataTokens["area"];
            return urlHelper.RouteUrl("Common_ModuleCssFile", new { area = area, fileName = cssFileName, moduleName = moduleName });
        }

        public static string CssPath(this UrlHelper urlHelper)
        {
            return CssPath(urlHelper, "");
        }

        public static string CssPath(this UrlHelper urlHelper, string relativePath)
        {
            if (!string.IsNullOrEmpty(relativePath) && relativePath.StartsWith("/"))
            {
                relativePath = relativePath.Remove(0, 1);
            }

            string[] locationsSearched;
            string moduleName = (string)urlHelper.RequestContext.HttpContext.Items["moduleName"];
            string styleSheetPath = ContentResolver.Current.GetStyleSheetPath(
                relativePath,
                urlHelper.RequestContext.RouteData.DataTokens["area"].ToString(),
                moduleName,
                out locationsSearched);

            if (string.IsNullOrEmpty(styleSheetPath))
            {
                LogHelper.Current.WriteWarning(string.Format("File '{0}' is not found. Searched locations: {1}", relativePath, locationsSearched.ToString()));
            }

            return urlHelper.AppPath(styleSheetPath);
        }

        #endregion

        #region Image path

        public static string ImagePath(this UrlHelper urlHelper)
        {
            return ImagePath(urlHelper, "");
        }

        public static string ImagePath(this UrlHelper urlHelper, string relativePath)
        {
            if (!string.IsNullOrEmpty(relativePath) && relativePath.StartsWith("/"))
            {
                relativePath = relativePath.Remove(0, 1);
            }

            string[] locationsSearched;
            string moduleName = (string)urlHelper.RequestContext.HttpContext.Items["moduleName"];
            string imagePath = ContentResolver.Current.GetImagePath(
                relativePath,
                urlHelper.RequestContext.RouteData.DataTokens["area"].ToString(),
                moduleName,
                out locationsSearched);

            if (string.IsNullOrEmpty(imagePath))
            {
                LogHelper.Current.WriteWarning(string.Format("File '{0}' is not found. Searched locations: {1}", relativePath, locationsSearched.ToString()));
                return urlHelper.AppPath("~/NotFound.jpg");
            }

            return urlHelper.AppPath(imagePath);
        }

        #endregion

        #region Script

        public static string ScriptPath(this UrlHelper urlHelper)
        {
            return ScriptPath(urlHelper, "");
        }

        public static string ScriptPath(this UrlHelper urlHelper, string relativePath)
        {
            if (!string.IsNullOrEmpty(relativePath) && relativePath.StartsWith("/"))
            {
                relativePath = relativePath.Remove(0, 1);
            }

            string[] locationsSearched;
            string moduleName = (string)urlHelper.RequestContext.HttpContext.Items["moduleName"];
            string scriptPath = ContentResolver.Current.GetScriptPath(
                relativePath,
                urlHelper.RequestContext.RouteData.DataTokens["area"].ToString(),
                moduleName,
                out locationsSearched);

            if (string.IsNullOrEmpty(scriptPath))
            {
                LogHelper.Current.WriteWarning(string.Format("File '{0}' is not found. Searched locations: {1}", relativePath, locationsSearched.ToString()));
            }

            return urlHelper.AppPath(scriptPath);
        }

        #endregion

        #region Page

        public static string BackOffice(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_Default");
        }

        public static string Home(this UrlHelper urlHelper)
        {
            return urlHelper.Page(SiteContext.Current.Site.DefaultPage);
        }

        public static string Pages(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_Pages");
        }

        public static string Page(this UrlHelper urlHelper, Guid pageId)
        {
            Page page = ContentManager.Current.GetPage(pageId);
            return urlHelper.Page(page.Name);
        }

        public static string Page(this UrlHelper urlHelper, string pageName)
        {
            return urlHelper.RouteUrl("StarterSite_Page", new { pageName = pageName });
        }

        public static string Page(this UrlHelper urlHelper)
        {
            return urlHelper.Page(SiteContext.Current.CurrentPage.Id);
        }

        public static string NewPage(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_NewPage");
        }

        public static string DeletePage(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_DeletePage", new { pageId = pageId });
        }

        public static string EditPage(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_EditPage", new { pageId = pageId });
        }

        public static string PageMoveUp(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_PageMoveUp", new { pageId = pageId });
        }

        public static string PageMoveDown(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_PageMoveDown", new { pageId = pageId });
        }

        #endregion

        #region Page Content

        public static string DeletePageContent(this UrlHelper urlHelper, int id)
        {
            return urlHelper.RouteUrl("BackOffice_PageContentDelete", new { id = id });
        }

        public static string RenamePageContent(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_RenamePageContent");
        }

        public static string PermissionsPageContent(this UrlHelper urlHelper, int id)
        {
            return urlHelper.RouteUrl("BackOffice_PermissionsPageContent", new { id = id });
        }

        public static string AddControlToPage(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_AddControlToPage", new { pageId = pageId });
        }

        public static string PageTemplate(this UrlHelper urlHelper, Guid pageId, string mode)
        {
            return urlHelper.RouteUrl("BackOffice_PageTemplate", new { pageId = pageId, mode = mode });
        }

        public static string PageTemplate(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_PageTemplate", new { pageId = pageId, mode = "View" });
        }

        public static string PageTemplate(this UrlHelper urlHelper, int pageContent)
        {
            var item = ContentManager.Current.GetPageContent(pageContent);

            return urlHelper.PageTemplate(item.PageId);
        }


        public static string AddModuleToPage(this UrlHelper urlHelper, Guid pageId, string commandName)
        {
            return urlHelper.RouteUrl("BackOffice_AddModuleToPage", new { pageId = pageId, commandName = commandName });
        }

        public static string SavePageContents(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_PageContentSave", new { pageId = pageId });
        }

        #endregion

        #region Module

        public static string Modules(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_Modules");
        }

        public static string NewModule(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_NewModule");
        }

        public static string InstallModule(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_InstallModule");
        }

        public static string UnInstallModule(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_UninstallModule", new { id = id });
        }

        public static string ModuleSettings(this UrlHelper urlHelper, int ctrId)
        {
            return urlHelper.RouteUrl("BackOffice_ModuleSettings", new { ctrId = ctrId });
        }

        public static string ModuleSettings(this UrlHelper urlHelper)
        {
            int ctrId = int.Parse(urlHelper.RequestContext.RouteData.Values["ctrId"].ToString());
            return urlHelper.RouteUrl("BackOffice_ModuleSettings", new { ctrId = ctrId });
        }

        #endregion

        #region UserSettings

        public static string Language(this UrlHelper urlHelper, string language)
        {
            return urlHelper.RouteUrl("LanguageGet", new { language = language });
        }

        public static string Language(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("LanguagePost");
        }

        #endregion

        #region Site

        public static string EditSite(this UrlHelper urlHelper, Guid siteId)
        {
            return urlHelper.RouteUrl("BackOffice_EditSite", new { siteId = siteId });
        }

        public static string RefreshCache(this UrlHelper urlHelper, Guid siteId)
        {
            return urlHelper.RouteUrl("BackOffice_RefreshCache", new { siteId = siteId });
        }

        #endregion

        #region Accounts

        public static string Users(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_Users");
        }

        public static string Users(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BackOffice_UsersPaging", new { pageNumber = pageNumber });
        }


        public static string EditUser(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_EditUser", new { id = id });
        }

        public static string DeleteUser(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_DeleteUser", new { id = id });
        }

        public static string LogOn(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_LogOn");
        }

        public static string LogOff(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_LogOff");
        }

        #endregion

        #region Logs

        public static string Logs(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_Logs");
        }

        public static string Logs(this UrlHelper urlHelper, int? pageNumber)
        {
            if (pageNumber.HasValue)
                return urlHelper.RouteUrl("BackOffice_LogsPaging", new { pageNumber = pageNumber.Value });
            else
                return urlHelper.Logs();
        }

        public static string DeleteLog(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_DeleteLog", new { id = id });
        }

        public static string LogDetails(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_LogDetails", new { id = id });
        }

        public static string ClearLogs(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_ClearLogs");
        }

        #endregion

        #region Error

        public static string Error(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("Error");
        }

        #endregion

        #region Module controls

        public static string LoginControl(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("StarterSite_LoginControl");
        }

        public static string LogOutControl(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("StarterSite_LogOutControl");
        }

        public static string Register(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("StarterSite_Register");
        }

        public static string Register(this UrlHelper urlHelper, string pageName)
        {
            return urlHelper.RouteUrl("StarterSite_Register", new { pageName = pageName });
        }

        public static string ActivateAccount(this UrlHelper urlHelper, string key)
        {
            return urlHelper.RouteUrl("StarterSite_ActivateAccount", new { key = key });
        }

        public static string ForgotPassword(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("StarterSite_ForgotPassword");
        }

        public static string ForgotPassword(this UrlHelper urlHelper, string pageName)
        {
            return urlHelper.RouteUrl("StarterSite_ForgotPassword", new { pageName = pageName });
        }

        public static string ChangePassword(this UrlHelper urlHelper, string key)
        {
            return urlHelper.RouteUrl("StarterSite_ChangePassword", new { key = key });
        }

        #endregion
    }
}
